package com.ruoyi.device.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.device.domain.DeviceUserRegister;
import com.ruoyi.device.mapper.DeviceUserRegisterMapper;
import com.ruoyi.device.service.IDeviceUserRegisterService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceUserRegisterServiceImpl implements IDeviceUserRegisterService {
    @Value("${wechart.userType")
    private  String userType;

    @Value("${wechart.userRole}")
    private String userRoleId;

    @Autowired
    private ISysUserService userService;

    @Autowired(required = false)
    private DeviceUserRegisterMapper registerMapper;

    /**
     * 获取app注册用户列表
     *
     * @param params
     * @return
     */
    @Override
    public List<DeviceUserRegister> getRegisterList (DeviceUserRegister params){
        return registerMapper.selectRegisterList(params);
    }
    /**
     * 获取app注册用户详情
     * @param id
     * @return
     */
    @Override
    public DeviceUserRegister getRegisterById(Long id){
        return registerMapper.selectRegisterById(id);
    }
    /**
     * 获取app注册用户 by 小程序openid
     * @param openid
     * @return
     */
    @Override
    public DeviceUserRegister getRegisterByOpenId(String openid){
        return registerMapper.selectRegisterByOpenId(openid);
    }
    /**
     * 获取app注册用户 by 用户名或者姓名或者手机号
     * @param param
     * @return
     */
    @Override
    public DeviceUserRegister getRegisterByName(String param){
        return registerMapper.selectRegisterByName(param);
    /**
     * 新增app注册用户
     * @param register
     * @return 结果
     */
    }
    @Override
    public int addRegister(DeviceUserRegister register) {
        register.setCreateTime(DateUtils.getNowDate());
        return registerMapper.insertRegister(register);
    }

    /**
     * 修改app注册用户
     * @param register
     * @return 结果
     */
    @Override
    public int editRegister(DeviceUserRegister register) {
        if(register.getRegStatus() ==1){
            SysUser user = new SysUser();
            user.setRoleId(Long.valueOf(userRoleId));
            user.setUserName(register.getRegCode());
            user.setNickName(register.getRegName());
            user.setDeptId(register.getRegDeptId());
            user.setPhonenumber(register.getRegMobile());
            user.setPassword(register.getRegPassword());
            user.setWxOpenid(register.getRegOpenid());
            user.setUserType(userType);
            userService.insertUser(user);
            register.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        }

        register.setCreateTime(DateUtils.getNowDate());
        return registerMapper.updateRegister(register);
    }
}
